package org.dandelion.core.remote
{
	import mx.messaging.Channel;
	import mx.messaging.ChannelSet;
	import mx.messaging.config.ServerConfig;
	
	import org.dandelion.config.DandelionCofig;
	import org.dandelion.core.channel.ChannelSetFactory;
	import org.dandelion.erro.DandelionError;
	import org.dandelion.erro.DandelionErrorInfo;
	
	/***
	 *	创建 DandelionRemoteObject实例
	 **/
	public class DandelionRemoteObjectFactory
	{
		/** */
		private static var _instance:DandelionRemoteObjectFactory;
		private var _channelSet:ChannelSet;
		
		public function DandelionRemoteObjectFactory()
		{
			if(_instance) throw new Error("Error: Singletons can only be instantiated via instance() method!");
			if (_channelSet == null)initChannel();
			DandelionRemoteObjectFactory._instance = this;
		}
		internal static function getInstance():DandelionRemoteObjectFactory
		{
			if (_instance == null) 
				_instance = new DandelionRemoteObjectFactory();
			return _instance;
		}
		
		/**
		 * 生成DandelionRemoteObject对象
		 * @param destination @see mx.rpc.RemoteObject.destination
		 * @param source @see mx.rpc.RemoteObject.source
		 * @return IDandelionRemoteObject
		 */
		internal function createDandelionRemoteObject(destination:String, source:String=""):IDandelionRemoteObject
		{
			var srService:DandelionRemoteObject = new DandelionRemoteObject();
			srService.destination = destination;
			srService.source = source;
			srService.channelSet = _channelSet;
			return srService;
		}
		
		/**
		 * 初始化AMFChannel
		 */
		private function initChannel():void
		{
//			_channel = new AMFChannel();
//			_channel.uri = DandelionCofig.AMFChannel;
//			if(!_channel.uri)
//				throw new DandelionError(DandelionErrorInfo.AMFCHANNEL_URI_IS_NULL);
			_channelSet = ChannelSetFactory.getInstance().getChannelSet(DandelionCofig.defaultAMFChannel);
		}
	}
}